package com.sjtu.streetplan.sqlite;

import java.sql.Timestamp;
import java.util.List;
import java.util.Random;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.sjtu.streetplan.R;
import com.sjtu.streetplan.net.Net;
import com.sjtu.streetplan.util.Position;

public class TestActivity extends Activity { 
	private SQLite sqlite;
	
    /** Called when the activity is first created. */ 
    private Button button1 = null;  
    private Button button2 = null;  
    private Button button3 = null;  
    private Button button4 = null;  
    private Button button5 = null;   
    private Button button6 = null;   
    private Button button7 = null;   
      
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.sqltest);  
        
        sqlite = new SQLite(this);
        
        //txtMsg=(TextView)findViewById(R.id.show);
       
        button1 = (Button) findViewById(R.id.btn_create);  
        button2 = (Button) findViewById(R.id.btn_insert);  
        button3 = (Button) findViewById(R.id.btn_delete);  
        button4 = (Button) findViewById(R.id.btn_update);  
        button5 = (Button) findViewById(R.id.btn_query);  
        button6 = (Button) findViewById(R.id.btn_drop);  
        button7 = (Button) findViewById(R.id.btn_send);  
 
        button1.setOnClickListener(new CreateListener());  
        button2.setOnClickListener(new InsertListener());  
        button3.setOnClickListener(new DeleteListener());  
        button4.setOnClickListener(new UpdateListener());  
        button5.setOnClickListener(new QueryListener());  
        button6.setOnClickListener(new DropListener());  
        button7.setOnClickListener(new SendListener()); 
 
    }  
 
    private class CreateListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	sqlite.createTable();
        }  
    }  

    private class InsertListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            // ��ȡ�ֻ����
            String phone = tm.getLine1Number();
            
            Timestamp time = new Timestamp(System.currentTimeMillis());
            Random random = new Random();    
            float lon = Math.abs(random.nextFloat());
            float lat = Math.abs(random.nextFloat());
            float angle = Math.abs(random.nextFloat());
            Position p = new Position(phone, time, lon, lat, angle);
            sqlite.insertItem(p);
        }  
    }  
    
    private class DeleteListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	sqlite.deleteAllItems();
        }  
    }  

    private class UpdateListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	sqlite.updateItem();  
        }  
    }
    
    private class QueryListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	sqlite.showItems();     
        }  
 
    }  
 
    private class DropListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {  
        	sqlite.dropTable();  
        }  
    }
    
    private class SendListener implements OnClickListener {  
        @Override 
        public void onClick(View v) {     
            try {     
            	List<Position> positions = sqlite.showItems();
            	Net.updateAllItems(positions);  
    			Log.i("Net", "���ͱ���ɹ���");
            } catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    			Log.i("Net", "���ͱ���ʧ�ܣ�");
    		}     
        }  
    }   
} 